package com.pudu.mydemo.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;

import android.view.View;
import android.view.ViewGroup;

import android.widget.ImageButton;

import android.widget.TextView;
import android.widget.Toast;

import com.pudu.mydemo.adapter.AbstractWheelTextAdapter;
import com.pudu.mydemo.view.OnWheelChangedListener;
import com.pudu.mydemo.view.OnWheelScrollListener;
import com.pudu.mydemo.view.WheelView;
import com.pudu.pdrobot.App;
import com.pudutech.pdrobot.R;

import java.util.ArrayList;
import java.util.Calendar;

/**
 * 日期选择对话框
 * 
 * @author pengjian
 *
 */
public class DateTimeActivity extends BaseHomeActivity {

	private static final int MIN_YEAR = 1970;
	private static final int MAX_YEAR = 2100;
	private ImageButton skip;
	private WheelView wvYear;
	private WheelView wvMonth;
	private WheelView wvDay;
	private WheelView wvHour;
	private WheelView wvMinute;

	private ImageButton btnSure;


	private ArrayList<String> arry_years = new ArrayList<String>();
	private ArrayList<String> arry_months = new ArrayList<String>();
	private ArrayList<String> arry_days = new ArrayList<String>();
	private ArrayList<String> arry_hours = new ArrayList<String>();
	private ArrayList<String> arry_minutes = new ArrayList<String>();

	private CalendarTextAdapter mYearAdapter;
	private CalendarTextAdapter mMonthAdapter;
	private CalendarTextAdapter mDaydapter;
	private CalendarTextAdapter mHourAdapter;
	private CalendarTextAdapter mMinuteAdapter;


	private int hour;
	private int minute;


	private int currentHour = 0;
	private int currentMinute = 0;
	private int month;
	private int day;

	private int currentYear = getYear();
	private int currentMonth = getMonth();
	private int currentDay = getDay();

	private int maxTextSize =32;
	private int minTextSize = 24;

	private boolean issetdata = false;

	private String selectYear;
	private String selectMonth;
	private String selectDay;
	private String selectHour;
	private String selectMinute;

	private String strTitle = "选择日期";

	private boolean isConnected;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		isConnected = intent.getBooleanExtra("isConnected",false);
		setContentView(R.layout.dialog_picker_center_real);
		init();
	}

	private void init() {
		skip = (ImageButton)findViewById(R.id.skip);
		wvYear = (WheelView)findViewById(R.id.year);
		wvMonth = (WheelView)findViewById(R.id.months);
		wvDay = (WheelView)findViewById(R.id.days);
		wvHour = (WheelView)findViewById(R.id.hours);
		wvMinute = (WheelView)findViewById(R.id.minutes);

		btnSure = (ImageButton) findViewById(R.id.btn_dialog_sure);

		btnSure.setOnClickListener(this);

		if (!issetdata) {
			initData();
		}
		initYears();
		mYearAdapter = new CalendarTextAdapter(this, arry_years,
				setYear(currentYear), maxTextSize, minTextSize);
		wvYear.setVisibleItems(3);
		wvYear.setViewAdapter(mYearAdapter);
		wvYear.setCurrentItem(setYear(currentYear));

		initMonths(month);
		mMonthAdapter = new CalendarTextAdapter(this, arry_months,
				setMonth(currentMonth), maxTextSize, minTextSize);
		wvMonth.setVisibleItems(3);
		wvMonth.setCyclic(true);
		wvMonth.setViewAdapter(mMonthAdapter);
		wvMonth.setCurrentItem(setMonth(currentMonth));

		initDays(day);
		mDaydapter = new CalendarTextAdapter(this, arry_days,
				currentDay - 1, maxTextSize, minTextSize);
		wvDay.setVisibleItems(3);
		wvDay.setCyclic(true);
		wvDay.setViewAdapter(mDaydapter);
		wvDay.setCurrentItem(currentDay - 1);

		initHours(hour);
		mHourAdapter = new CalendarTextAdapter(this, arry_hours,
				currentHour, maxTextSize, minTextSize);
		wvHour.setVisibleItems(3);
		wvHour.setViewAdapter(mHourAdapter);
		wvHour.setCurrentItem(currentHour);

		initMinutes(minute);
		mMinuteAdapter = new CalendarTextAdapter(this, arry_minutes,
				currentMinute, maxTextSize, minTextSize);
		wvMinute.setVisibleItems(3);
		wvMinute.setViewAdapter(mMinuteAdapter);
		wvMinute.setCurrentItem(currentMinute);

		wvYear.addChangingListener(new OnWheelChangedListener() {

			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// TODO Auto-generated method stub
				String currentText = (String) mYearAdapter.getItemText(wheel
						.getCurrentItem());
				selectYear = currentText;
				setTextviewSize(currentText, mYearAdapter);
				currentYear = Integer.parseInt(currentText);
				setYear(currentYear);
				initMonths(month);
				mMonthAdapter = new CalendarTextAdapter(DateTimeActivity.this, arry_months,
						0, maxTextSize, minTextSize);
				wvMonth.setVisibleItems(3);
				wvMonth.setViewAdapter(mMonthAdapter);
				wvMonth.setCurrentItem(0);

//				Log.e("guiliu","onchange selectYear"+selectYear);
			}
		});

		wvYear.addScrollingListener(new OnWheelScrollListener() {

			@Override
			public void onScrollingStarted(WheelView wheel) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onScrollingFinished(WheelView wheel) {
				// TODO Auto-generated method stub
				String currentText = (String) mYearAdapter.getItemText(wheel
						.getCurrentItem());
				setTextviewSize(currentText, mYearAdapter);
//				Log.e("guiliu","onScrollingFinished currentText"+currentText);
			}
		});

		wvMonth.addChangingListener(new OnWheelChangedListener() {

			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// TODO Auto-generated method stub
				String currentText = (String) mMonthAdapter.getItemText(wheel
						.getCurrentItem());
				selectMonth = currentText;
				setTextviewSize(currentText, mMonthAdapter);
				setMonth(Integer.parseInt(currentText));
				initDays(day);
				mDaydapter = new CalendarTextAdapter(DateTimeActivity.this, arry_days, 0,
						maxTextSize, minTextSize);
				wvDay.setVisibleItems(3);
				wvDay.setViewAdapter(mDaydapter);
				wvDay.setCurrentItem(0);
			}
		});

		wvMonth.addScrollingListener(new OnWheelScrollListener() {

			@Override
			public void onScrollingStarted(WheelView wheel) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onScrollingFinished(WheelView wheel) {
				// TODO Auto-generated method stub
				String currentText = (String) mMonthAdapter.getItemText(wheel
						.getCurrentItem());
				setTextviewSize(currentText, mMonthAdapter);
			}
		});

		wvDay.addChangingListener(new OnWheelChangedListener() {

			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// TODO Auto-generated method stub
				String currentText = (String) mDaydapter.getItemText(wheel
						.getCurrentItem());
				setTextviewSize(currentText, mDaydapter);
				selectDay = currentText;
			}
		});

		wvDay.addScrollingListener(new OnWheelScrollListener() {

			@Override
			public void onScrollingStarted(WheelView wheel) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onScrollingFinished(WheelView wheel) {
				// TODO Auto-generated method stub
				String currentText = (String) mDaydapter.getItemText(wheel
						.getCurrentItem());
				setTextviewSize(currentText, mDaydapter);
			}
		});

		wvHour.addChangingListener(new OnWheelChangedListener() {

			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// TODO Auto-generated method stub
				String currentText = (String) mHourAdapter.getItemText(wheel
						.getCurrentItem());
				setTextviewSize(currentText, mHourAdapter);
				selectHour = currentText;
			}
		});

		wvHour.addScrollingListener(new OnWheelScrollListener() {

			@Override
			public void onScrollingStarted(WheelView wheel) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onScrollingFinished(WheelView wheel) {
				// TODO Auto-generated method stub
				String currentText = (String) mHourAdapter.getItemText(wheel
						.getCurrentItem());
				setTextviewSize(currentText, mHourAdapter);
			}
		});

		wvMinute.addChangingListener(new OnWheelChangedListener() {

			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// TODO Auto-generated method stub
				String currentText = (String) mMinuteAdapter.getItemText(wheel
						.getCurrentItem());
				setTextviewSize(currentText, mMinuteAdapter);
				selectMinute = currentText;
			}
		});

		wvMinute.addScrollingListener(new OnWheelScrollListener() {

			@Override
			public void onScrollingStarted(WheelView wheel) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onScrollingFinished(WheelView wheel) {
				// TODO Auto-generated method stub
				String currentText = (String) mMinuteAdapter.getItemText(wheel
						.getCurrentItem());
				setTextviewSize(currentText, mMinuteAdapter);
			}
		});

		skip.setOnClickListener(this);
	}

	public void initData() {
		setDate(getYear(), getMonth(), getDay(),getHour(), getMinute());
	}

	public void initYears() {
		for (int i = MIN_YEAR; i < MAX_YEAR; i++) {
			arry_years.add(i + "");
		}
	}

	public void initMonths(int months) {
		arry_months.clear();
		for (int i = 1; i <= months; i++) {
			if( i <10) {
				arry_months.add("0"+i);
			} else {
				arry_months.add(i + "");
			}

		}
	}

	public void initDays(int days) {
		arry_days.clear();
		for (int i = 1; i <= days; i++) {
			if(i < 10) {
				arry_days.add("0"+i);
			} else {
				arry_days.add(i + "");
			}

		}
	}

	public void initHours(int hours) {
		arry_hours.clear();
		for (int i = 0; i < hours; i++) {
			if (i < 10) {
				arry_hours.add("0" + i);
			} else {
				arry_hours.add(i + "");
			}
		}
	}

	public void initMinutes(int minutes) {
		arry_minutes.clear();
		for (int i = 0; i < minutes; i++) {
			if (i < 10) {
				arry_minutes.add("0" + i);
			} else {
				arry_minutes.add(i + "");
			}
		}
	}

	public void setTitle(String title) {
		this.strTitle = title;
	}




	@Override
	public void onClick(View v) {
		super.onClick(v);
		Intent intent = new Intent();
		if(v == btnSure) {
			setSystemDate();
		}
		if (v == btnSure || v == skip) {
			if(!isConnected) {
				intent.setAction("android.intent.action.welcome");
			} else if(!mapSet()) {
				intent.setClass(DateTimeActivity.this,MapUpLoadActivity.class);
			} else {
				intent.setClass(DateTimeActivity.this,HomeActivity.class);
			}
			startActivity(intent);
			finish();
		}
	}


	public void setSystemDate() {
		//calender.set无效，无法修改系统时间，需要root权限或者系统进程
		// android:shareduserid="android.uid.system"
		//<uses-permission android:name="android.permission.SET_TIME" />
		//系统签名
		//android.mk LOCAL_CERTIFICATE := platform
		Calendar calendar = Calendar.getInstance();
		calendar.set(Integer.parseInt(selectYear), Integer.parseInt(selectMonth) -1, Integer.parseInt(selectDay) , Integer.parseInt(selectHour), Integer.parseInt(selectMinute), 0);
		long when = calendar.getTimeInMillis();
		if(when / 1000 <Integer.MAX_VALUE) {
			SystemClock.setCurrentTimeMillis(when);
		}
	}

	public String getClickAction(View v) {
		if(v == btnSure) {
			return "确认时间";
		} else if(v == skip) {
			return "跳过";
		}

		return "";
	}
	public boolean mapSet() {
		String map_data = App.getInstance().getApplicationMapData();
		String altas_data = App.getInstance().getApplicationAtlasData();
		if(map_data != null && map_data.length() > 0 && altas_data != null && altas_data.length() > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 设置字体大小
	 * 
	 * @param curriteItemText
	 * @param adapter
	 */
	public void setTextviewSize(String curriteItemText,
			CalendarTextAdapter adapter) {
		ArrayList<View> arrayList = adapter.getTestViews();
		int size = arrayList.size();
		String currentText;
		for (int i = 0; i < size; i++) {
			TextView textvew = (TextView) arrayList.get(i);
			currentText = textvew.getText().toString();
			if (curriteItemText.equals(currentText)) {
				textvew.setTextSize(maxTextSize);
//				textvew.setTextColor(getResources().getColor(R.color.gold));
			} else {
				textvew.setTextSize(minTextSize);
			}
		}
	}

	public int getYear() {
		Calendar c = Calendar.getInstance();
		return c.get(Calendar.YEAR);
	}

	public int getMonth() {
		Calendar c = Calendar.getInstance();
		return c.get(Calendar.MONTH) + 1;
	}

	public int getDay() {
		Calendar c = Calendar.getInstance();
		return c.get(Calendar.DATE);
	}


	public int getHour() {
		Calendar c = Calendar.getInstance();
		Log.e("guiliu","hour"+c.get(Calendar.HOUR_OF_DAY));
		return c.get(Calendar.HOUR_OF_DAY);
	}

	public int getMinute() {
		Calendar c = Calendar.getInstance();
		return c.get(Calendar.MINUTE);
	}
	/**
	 * 设置年月日
	 * 
	 * @param year 2016
	 * @param month 8
	 * @param day 2
	 */
	public void setDate(int year, int month, int day, int hour, int minute) {
		selectYear = year + "";
		selectMonth = month + "";
		selectDay = day + "";
		selectHour = hour + "";
		selectMinute = minute + "";
		issetdata = true;
		this.currentYear = year;
		this.currentMonth = month;
		this.currentDay = day;
		this.month = 12;
		this.currentHour = hour;
		this.currentMinute = minute;
		this.hour = 24;
		this.minute = 60;
		calDays(year, month);
	}

	/**
	 * 设置年份
	 * 
	 * @param year
	 */
	private int setYear(int year) {
		int yearIndex = 0;
		for (int i = MIN_YEAR; i < MAX_YEAR; i++) {
			if (i == year) {
				return yearIndex;
			}
			yearIndex++;
		}
		return yearIndex;
	}

	/**
	 * 设置月份
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	private int setMonth(int month) {
		int monthIndex = 0;
		calDays(currentYear, month);
		for (int i = 1; i < this.month; i++) {
			if (month == i) {
				return monthIndex;
			} else {
				monthIndex++;
			}
		}
		return monthIndex;
	}

	/**
	 * 计算每月多少天
	 * 
	 * @param month
	 * @param leayyear 2016  8 ->30
	 */
	public void calDays(int year, int month) {
		boolean leayyear = false;
		if (year % 4 == 0 && year % 100 != 0) {
			leayyear = true;
		} else {
			leayyear = false;
		}
		for (int i = 1; i <= 12; i++) {
			switch (month) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				this.day = 31;
				break;
			case 2:
				if (leayyear) {
					this.day = 29;
				} else {
					this.day = 28;
				}
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				this.day = 30;
				break;
			}
		}
		/*if (year == getYear() && month == getMonth()) {
			this.day = getDay();
		}*/


	}

	private class CalendarTextAdapter extends AbstractWheelTextAdapter {

		ArrayList<String> list;

		protected CalendarTextAdapter(Context context, ArrayList<String> list,
				int currentItem, int maxsize, int minsize) {
			super(context, R.layout.item_birth_year, NO_RESOURCE, currentItem,
					maxsize, minsize);
			this.list = list;
			setItemTextResource(R.id.tempValue);
		}

		@Override
		public View getItem(int index, View cachedView, ViewGroup parent) {
			View view = super.getItem(index, cachedView, parent);
			return view;
		}

		@Override
		public int getItemsCount() {
			return list.size();
		}

		@Override
		protected CharSequence getItemText(int index) {
			return list.get(index) + "";
		}
	}

}